<?php

namespace App\Admin\Controller\Base;


use App\Admin\Service\Base\UpdateCURDService;
use App\Common\Exception\ApiMessageException;
use App\Common\Util\Response;
use App\Common\Util\TP;
use plugins\Dev\Admin\Service\DevAuxService;
use plugins\Dev\Admin\Service\MakeCURDService;
use think\annotation\route\Get;

/**
 * 开发辅助
 *
 * Class DevAxuController
 */
class DevAuxController
{
    /**
     * 基础文件创建
     *
     * @return string
     */
    public function baseFileMake(): string
    {
        return Response::render("Admin.Base.Dev.file-make", [
            'plugins' => TP::config('plugins.lists')
        ]);
    }

    /**
     * 获取字段详情
     *
     * @return \think\Response
     */
    public function getTableFields(): \think\Response
    {
        $table = TP::request()->get('table');

        $fields = DevAuxService::aop()->getFields($table);

        return Response::json()->success($fields);
    }

    /**
     * 获取所有表
     *
     * @return \think\Response
     */
    #[Get('get-all-tables')]
    public function getAllTables(): \think\Response
    {
        $tableInfo = DevAuxService::aop()->getAllTableInfo();

        return Response::json()->success($tableInfo);
    }


    /**
     * 创建CURD
     *
     * @return \think\Response
     */
    public function makeCURD(): \think\Response
    {
        $res = (new MakeCURDService(TP::request()->post()))->make();

        return Response::json()->success($res);
    }

    /**
     * 检查已存在的文件
     *
     * @return \think\Response
     * @throws \Exception
     */
    public function checkCURDFiles(): \think\Response
    {
        $res = MakeCURDService::check(
            TP::request()->get('table'),
            TP::request()->get('dir'),
            TP::request()->get('plugins'),
        );

        return Response::json()->success($res);
    }

    /**
     * 清除CURD文件
     *
     * @return \think\Response
     */
    public function CURDClear(): \think\Response
    {
        MakeCURDService::clear(TP::request()->post('files'));

        return Response::json()->success();
    }

    /**
     * 获取已有的文件夹
     *
     * @return \think\Response
     */
    public function getMakeDirs(): \think\Response
    {
        return Response::json()->success(
            MakeCURDService::getCurrentDirs(TP::request()->query('plugins'))
        );
    }

    /**
     * 更新文件
     *
     * @return string
     */
    public function baseFileUpdate(): string
    {
        return Response::render('Admin.Base.Dev.file-update', [
            'plugins' => TP::config('plugins.lists')
        ]);
    }


    /**
     * 检测更新的文件
     *
     * @return \think\Response
     */
    public function updateFileCheck(): \think\Response
    {
        $result = (new UpdateCURDService(
            (string)TP::request()->get('table'),
            (string)TP::request()->get('dir'),
            (string)TP::request()->get('plugins'),
        ))->check();

        return Response::json()->success($result);
    }

    /**
     * 执行文件更新
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function updateFileExec(): \think\Response
    {
        (new UpdateCURDService(
            (string)TP::request()->post('table'),
            (string)TP::request()->post('dir'),
            (string)TP::request()->post('plugins'),
        ))->exec(
            (string)TP::request()->post('ModelCode'),
        );

        return Response::json()->success();
    }
}